package com.example.human_resource_management.controller;

import com.example.human_resource_management.common.ResponseData;
import com.example.human_resource_management.config.JwtUtils;
import com.example.human_resource_management.entry.dto.LoginDto;
import com.example.human_resource_management.entry.po.User;
import com.example.human_resource_management.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/user")
public class UserController {
    @Autowired
    private UserService userService;

    //员工token登录
    @PostMapping("login")
    public ResponseData<String> login(@RequestBody LoginDto loginDto) {
        User user = userService.login(loginDto);
        if (user != null) {
            String token = JwtUtils.generateToken(Long.valueOf(user.getEmpId()));
            return ResponseData.ok(token);
        } else {
            return ResponseData.warn("用户名或密码错误");
        }
    }
}
//